<template>
  <div>
    <el-card shadow="never">
      <h3>动态管理</h3>
    </el-card>

    <br>
    <data-table :table-data="tableData">
      <el-table-column
        label="编号"
        align="center"
        width="80"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="发表用户"
        align="center"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.user == null ? '佚名' : scope.row.user.nickName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="内容"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.content }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="图片"
        align="center"
      >
        <template slot-scope="scope">
          <div v-if="scope.row.postImgList&&scope.row.postImgList.length>0">
            <img
              v-for="item in scope.row.postImgList"
              :key="item.id"
              style="margin-left:8px;"
              height="50"
              width="50"
              :src="item.url"
              alt=""
            >
          </div>
          <div v-if="scope.row.postImgList.length==0">无</div>
        </template>
      </el-table-column>

      <el-table-column
        label="发表时间"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.createTime | formatDate2 }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="#"
        align="center"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="danger" plain @click="delBtn(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </data-table>
    <br>

  </div>
</template>

<script>
import { delPost, listAll } from '@/api/post'
import DataTable from '@/components/DataTable'

export default {

  name: 'PostManage',
  components: {
    DataTable
  },
  data() {
    return {
      addVisible: false,
      editVisible: false,
      form: {},
      tableData: []
    }
  },
  created() {
    this.getData()
  },
  methods: {
    getData() {
      listAll().then(resp => {
        debugger
        this.tableData = resp.data
      })
    },

    editBtn(row) {
      this.$router.push(`/admin/post/edit/${row.id}`)
      this.form = { ...row }
      this.editVisible = true
    },

    delBtn(id) {
      this.$confirm('确认要删除吗？', '提示', {
        type: 'error',
        confirmButtonText: '确认',
        cancelButtonText: '取消'
      }).then(() => {
        delPost(id).then(resp => {
          this.$notifySuccess(resp.msg)
          this.getData()
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.center {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
